var express = require('express');
var router = express.Router();

var db = require('./../../utils/db.js');

/**
 * 项目流程-模块 控制器
 */

router.post('/moduleTree', function (req, res, next) {
    var parentId = req.body.parentId;
    var sql = '';
    if (parentId && parseInt(parentId) !== 0) {
        sql = "SELECT parent.id AS id, parent.module_name AS name, parent.parent_id AS pId, (SELECT COUNT(child.id) FROM project_progress_module AS child WHERE child.parent_id=parent.id) AS isParent FROM project_progress_module AS parent WHERE parent.parent_id = " + parentId + " ORDER BY parent.module_name ASC";
    } else {
        sql = "select parent.id AS id, parent.module_name AS name, parent.parent_id AS pId, (SELECT COUNT(child.id) FROM project_progress_module AS child WHERE child.parent_id=parent.id) AS isParent from project_progress_module AS parent where depth=0 ORDER BY module_name ASC";
    }

    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.json(rows);

        } else {
            res.json({
                errcode: 1,
                message: '获取项目模块树失败'
            });
        }

    });
});

/**
 * 列表
 */

router.get('/moduleList', function (req, res, next) {
    var keyword = req.query.keyword;

    var sql = '';
    if (keyword) {
        sql = "select parent.id AS id, parent.module_name AS name, parent.parent_id AS pId from project_progress_module AS parent where parent.module_name like '%" + keyword + "%' ORDER BY parent.module_name ASC";
    } else {
        sql = "select parent.id AS id, parent.module_name AS name, parent.parent_id AS pId from project_progress_module AS parent ORDER BY parent.module_name ASC";
    }


    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.json(rows);

        } else {
            res.json({
                errcode: 1,
                message: '获取项目模块列表失败'
            });
        }

    });
});

/**
 * 保存
 */
router.post('/save', function (req, res, next) {
    var moduleName = req.body.moduleName ? req.body.moduleName.trim() : '';
    var parentId = req.body.parentId ? req.body.parentId.trim() : '';
    var depth = req.body.depth ? req.body.depth.trim() : '';

    var id = req.body.id;
    var sql = '';
    if (id) {
        sql = "update project_progress_module set module_name='" + moduleName + "',parent_id=" + parentId + " where id=" + id;
    } else {
        sql = "insert into project_progress_module(module_name,parent_id,depth,create_time,create_by_id,remark) values ('" + moduleName + "'," + parentId + "," + depth + ",'2018-03-06 15:57:45',1,'')";
    }

    db.query(sql, function (err, rows) {
        if (!err) {
            res.redirect("/process/requirement/index");
        } else {
            res.json({
                errcode: 1,
                message: '保存失败'
            });
        }
    });


});

/**
 * 删除
 */
router.post('/delete/:id', function (req, res, next) {
    var id = req.params.id;
    var sql = '';
    if (id) {
        sql = "delete from project_progress_module where id=" + id;
        db.query(sql, function (err, rows) {
            if (!err) {
                res.json({
                    errcode: 0,
                    message: '删除成功'
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '删除失败'
                });
            }
        });
    } else {
        res.json({
            errcode: 2,
            message: '系统错误'
        });
    }


});

module.exports = router;
